package com.week2.day04.teamFight;

import java.util.Random;

public class FightRules {
    boolean team1IsAlive = false;
    boolean team2IsAlive = false;

    public FightRules() {
    }

    public boolean aliveJudge(Hero[] team) {
        for (int i = 0; i < team.length; i++) {
            if (team[i].isAlive()) {
                return true;
            }
        }
        return false;
    }

    public Hero teamFighter(Hero[] team) {
        for (int i = 0; i < team.length; i++) {
            if (team[i].isAlive()) {
                return team[i];
            }
        }
        return new Hero(0, 0);
    }

    public void fight(int num) {
        Hero[] team1 = new Hero().teamUp(num);
        Hero[] team2 = new Hero().teamUp(num);
        team1IsAlive = aliveJudge(team1);
        team2IsAlive = aliveJudge(team2);
        int round = 1;
        while (team1IsAlive && team2IsAlive) {
            Random random = new Random();
            System.out.println("第"+(round++)+"回合");
            if (random.nextInt(2) == 0) {
                teamFighter(team2).attack(teamFighter(team1));
                teamFighter(team1).attack(teamFighter(team2));
                System.out.println("队伍1先手");
            }else {
                teamFighter(team1).attack(teamFighter(team2));
                teamFighter(team2).attack(teamFighter(team1));
                System.out.println("队伍2先手");
            }
            System.out.println("队伍1:" + teamFighter(team1).toString() + "\n队伍2:" + teamFighter(team2).toString());
            team1IsAlive = aliveJudge(team1);
            team2IsAlive = aliveJudge(team2);
        }
        if (team1IsAlive) {
            System.out.println("team1获胜");
        } else {
            System.out.println("team2获胜");
        }
    }
}
